package com.hz.framework.ajax;

import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import com.hz.framework.MyBean.PageQuery;

/***
 * DWR 分页方法类
 * **/
public class JsonPage extends PageQuery{
	public JSONObject jsonPaging(JSONObject map){
	     JSONObject object = new JSONObject();
//	   总页数
	     int total = 0;
//	 每页的起始行数
	     int pageStartRow = 0;
//	 是否有下一页
	     String hasNextPage = "false";
//	 是否有前一页
	     String hasPreviousPage =  "false";
	  
	     int thisCount = Integer.parseInt((String)map.get("thisCount"));
	     int lineCount = Integer.parseInt((String)map.get("lineCount")); 
	     String  action    = (String)map.get("action");
	     String state = (String)map.get("state");
	     String nextPic = (String)map.get("nextPic");
	     String previousPic = (String)map.get("previousPic");
	     String first = (String)map.get("first");
	     String end = (String)map.get("end");
	     if("first".equalsIgnoreCase(action)){
	         thisCount = 1;
	     }else if("next".equalsIgnoreCase(action)){
	         thisCount = thisCount+1;
	     }else if("previous".equalsIgnoreCase(action)){
	         thisCount = thisCount-1;
	     }
	     String sql = AjaxSQL.doAjaxSQL((String)map.get("sql"))+(String)map.get("where");
	     String peo="";
	     //拼装sql
	     String blon= (String) map.get("blon");
			if(blon.equals("true")){
				peo="select new map( ";
				String commons = (String)map.get("commons");
				String[] common= commons.split(",");
				for(int i=0;i<common.length;i++){
					peo +=common[i]+" as "+common[i]+ ",";
				}
				peo= peo.substring(0, peo.lastIndexOf(",")) + " )";
			}
		 sql = peo + sql;
	     //System.out.println((String)map.get("sql")+(String)map.get("where"));
	     System.out.println(sql);
	     sql = sql.replaceAll("<<", "%");
	     sql = sql.replaceAll(">>", "%");
	     //总行数
	     int rowCount = 0;
	     if(state.equals("sql")){
	    	 rowCount = getRowCountSql(sql);
	     }else if(state.equals("jdbc")){
	    	 rowCount = getRowCountJdbc(sql);
	     }else{
	    	 rowCount = getRowCountHql(sql);
	     }
	     //总页数
	     if ((rowCount % lineCount) == 0) { 
	         total = rowCount / lineCount; 
	     }
	     else {
	         total = rowCount / lineCount + 1;  
	     }
	     
	     /**根据当前页数 和总页数  判断 是否有下一页*/
	     if (thisCount >= total) {  
	         hasNextPage =  "false";  
	     }
	     else { 
	         hasNextPage = "true";  
	     }
	     if(thisCount>1){
	         hasPreviousPage ="true";
	     }
	     
	     //起始行数
	     if (thisCount * lineCount < rowCount) {
	         pageStartRow = thisCount * lineCount-lineCount; 
	     }
	     else { 
	         if(total==0){
	             total=1;
	         }
	         pageStartRow = lineCount * (total - 1); 
	        
	     }
	     //int a=1; //此参数为以后多bean调用分页时做以区分
	     //页面数据
	     List list = new ArrayList();
	     if(state.equals("sql")){
	    	 list = querySqlList(sql, pageStartRow,lineCount);
	     }else if(state.equals("jdbc")){
	    	 list = queryJdbcList(sql, pageStartRow,lineCount);
	     }else{
	    	 list = queryHqlList(sql, pageStartRow,lineCount);
	     }
	     //list=getPagelist(list,a);
	     object.put("pagelist", list);
	     String pageDiv = pagingDiv(thisCount, total, lineCount, rowCount, hasNextPage, hasPreviousPage, nextPic, previousPic,first,end);
	     object.put("pagediv",pageDiv);
	     String pageDivhide = pagingDivHide(thisCount, total, lineCount, rowCount, hasNextPage, hasPreviousPage, nextPic, previousPic,first,end);
	     object.put("pagedivhide",pageDivhide);
	     return object;
	 }
	 
   public String pagingDiv(int thisCount,int total,int lineCount,int rowCount,String hasNextPage,String hasPreviousPage, String nextPic , String previousPic, String first, String end){
       
      StringBuffer page = new StringBuffer();
      page.append("<TABLE width=\"100%\">");
      page.append("<TR style=\"font-size:12px;\">");
      page.append("<TD width=\"3%\">&nbsp;</TD>");
      if(rowCount==0){
    	  page.append("<TD align=\"right\">&nbsp;</TD>");
    	  page.append("<TD width=\"\" align=\"left\" valign=\"middle\">暂时没有您可以浏览的信息</TD>");
      }else{
      page.append("<TD width=\"\" align=\"left\" valign=\"middle\">共<FONT color=\"#849ED6\"><B>");
      page.append(rowCount);
      page.append("</B></FONT>条数据，共<FONT color=\"#849ED6\"><B>");
      page.append(total);
      page.append("</B></FONT>页。当前是第<FONT color=\"#849ED6\">");
      page.append("<SELECT style=\"width:50px;\" name=\"jsonPage_page\" onChange=\"on_Jsonpaging_IndexPage()\">");
      
      if ("0".equals(String.valueOf(thisCount))) {
          page.append("<option vaue=\"0\">0</option>");
      }
      for (int i = 1; i < total + 1; i++) {

          if (i == thisCount) {
              page.append("<OPTION value=\"");
              page.append(i);
              page.append("\" selected >");
              page.append(i);
              page.append("</OPTION>");
          } else {
              page.append("<OPTION value=\"");
              page.append(i);
              page.append("\" >");
              page.append(i);
              page.append("</OPTION>");
          }
      }
      page.append("</SELECT> </FONT>页。</TD>");
      page.append("<TD align=\"left\">&nbsp;</TD>");
      if ("true".equals(hasPreviousPage) ) {
          page.append("<TD align=\"left\"><IMG name=\"first\" title=\"首页\" src='"+first+"' onClick=\"on_Jsonpaging_FirstEnd(1)\" style=\"cursor:hand;\"> <IMG name=\"PgUp\" title=\"上一页\"");
          page.append("src=\"");
          page.append(previousPic);
          page.append("\" style=\"cursor:hand;\"");
          page.append("onClick=\"on_Jsonpaging_PreviousPage()\"></TD>");
      }else{
    	  page.append("<TD align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>");
      }
      if ( "true".equals(hasNextPage) ) {
          page.append("<TD align=\"left\"><IMG name=\"PgUp\" title=\"下一页\"");
          page.append("src=\"");
          page.append(nextPic);
          page.append("\" style=\"cursor:hand;\"");
          page.append("onClick=\"on_Jsonpaging_NextPage()\"> <IMG name=\"offal\" title=\"尾页\" src='"+end+"' onClick=\"on_Jsonpaging_FirstEnd('"+total+"')\" style=\"cursor:hand;\"></TD>");
      }else{
    	  page.append("<TD align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>");
      }
      }
      //page.append("<TD align=\"right\">&nbsp;</TD>");
      page.append("<TD width=\"40%\">&nbsp;</TD>");

      page.append("</TR>");
      page.append("</TABLE>");
      return page.toString();
   }
   
   public String pagingDivHide(int thisCount,int total,int lineCount,int rowCount,String hasNextPage,String hasPreviousPage, String nextPic , String previousPic, String first, String end){
       
	      StringBuffer page = new StringBuffer();
	      page.append("<TABLE width=\"100%\">");
	      page.append("<TR style=\"font-size:12px;\">");
	      page.append("<TD width=\"3%\">&nbsp;</TD>");
	      if(rowCount==0){
	    	  page.append("<TD align=\"right\">&nbsp;</TD>");
	    	  page.append("<TD width=\"\" align=\"left\" valign=\"middle\">暂时没有您可以浏览的信息</TD>");
	      }else{
	      page.append("<TD width=\"\" align=\"left\" valign=\"middle\" style='display: none;'>共<FONT color=\"#849ED6\"><B>");
	      page.append(rowCount);
	      page.append("</B></FONT>条数据，共<FONT color=\"#849ED6\"><B>");
	      page.append(total);
	      page.append("</B></FONT>页。当前是第<FONT color=\"#849ED6\">");
	      page.append("<SELECT style=\"width:50px;\" name=\"jsonPage_page\" onChange=\"on_Jsonpaging_IndexPage()\">");
	      
	      if ("0".equals(String.valueOf(thisCount))) {
	          page.append("<option vaue=\"0\">0</option>");
	      }
	      for (int i = 1; i < total + 1; i++) {

	          if (i == thisCount) {
	              page.append("<OPTION value=\"");
	              page.append(i);
	              page.append("\" selected >");
	              page.append(i);
	              page.append("</OPTION>");
	          } else {
	              page.append("<OPTION value=\"");
	              page.append(i);
	              page.append("\" >");
	              page.append(i);
	              page.append("</OPTION>");
	          }
	      }
	      page.append("</SELECT> </FONT>页。</TD>");
	      page.append("<TD align=\"left\">&nbsp;</TD>");
	      if ("true".equals(hasPreviousPage) ) {
	          page.append("<TD align=\"left\"><IMG name=\"first\" title=\"首页\" src='"+first+"' onClick=\"on_Jsonpaging_FirstEnd(1)\" style=\"cursor:hand;\"> <IMG name=\"PgUp\" title=\"上一页\"");
	          page.append("src=\"");
	          page.append(previousPic);
	          page.append("\" style=\"cursor:hand;\"");
	          page.append("onClick=\"on_Jsonpaging_PreviousPage()\"></TD>");
	      }else{
	    	  page.append("<TD align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>");
	      }
	      if ( "true".equals(hasNextPage) ) {
	          page.append("<TD align=\"left\"><IMG name=\"PgUp\" title=\"下一页\"");
	          page.append("src=\"");
	          page.append(nextPic);
	          page.append("\" style=\"cursor:hand;\"");
	          page.append("onClick=\"on_Jsonpaging_NextPage()\"> <IMG name=\"offal\" title=\"尾页\" src='"+end+"' onClick=\"on_Jsonpaging_FirstEnd('"+total+"')\" style=\"cursor:hand;\"></TD>");
	      }else{
	    	  page.append("<TD align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>");
	      }
	      }
	      //page.append("<TD align=\"right\">&nbsp;</TD>");
	      page.append("<TD width=\"40%\">&nbsp;</TD>");

	      page.append("</TR>");
	      page.append("</TABLE>");
	      return page.toString();
	   }
}
